package com.cdc.dimen;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;
import com.cdc.permission.PermissionsActivity;
import com.cdc.permission.PermissionsChecker;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class InitActivity extends AppCompatActivity {
    private final int SPLASH_DISPLAY_LENGTH = 2000; // 延迟时间
    private static final int REQUEST_CODE = 0; // 请求码
    private Context context = null;
    // 所需的全部权限
    private static final String[] PERMISSIONS = new String[]{
            Manifest.permission.INTERNET,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,//写入存储
            Manifest.permission.ACCESS_FINE_LOCATION,//位置信息
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA //相机
    };

    private PermissionsChecker mPermissionsChecker; // 权限检测
    private View contentMain;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_init);
        String tempPath=context.getFilesDir().getAbsolutePath()+"/"+"OperationalLayers";
        File file=new File(tempPath);
        if(!file.exists()){
            copyAssertDirToData(this,"OperationalLayers",tempPath);
        }
        mPermissionsChecker = new PermissionsChecker(this);
        // 缺少权限时, 进入权限配置页面
        if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
            startPermissionsActivity();
        } else {
            goMain();
        }
        TextView tvName = (TextView) this.findViewById(R.id.tvName);
        contentMain = this.findViewById(R.id.contentMain);
        TextView tvVersionName = (TextView) this.findViewById(R.id.tvVersionName);
        try {
            PackageManager packageManager = this.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    this.getPackageName(), 0);
            tvVersionName.setText("版本号:" + packageInfo.versionName + "_" + packageInfo.versionCode);
            tvName.setText(this.getResources().getString(packageInfo.applicationInfo.labelRes));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 弹出权限获取提示信息
     */
    private void startPermissionsActivity() {
        PermissionsActivity.startActivityForResult(this, REQUEST_CODE, PERMISSIONS);
    }

    private void goMain() {
        new Handler(Looper.myLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }

    /**
     * 跳转
     */
    private void startActivity() {
        Intent mainIntent = new Intent(context, MainActivity.class);
        context.startActivity(mainIntent);
        ((Activity) context).finish();
    }

    private void copyAssertDirToData(Context context, String assetDir, String dir) {
        String[] files;
        try {
            files = context.getResources().getAssets().list(assetDir);
        } catch (IOException e1) {
            return;
        }
        File mWorkingPath = new File(dir);
        if (!mWorkingPath.exists()) {
            mWorkingPath.mkdirs();
        }
        for (int i = 0; i < files.length; i++) {
            try {
                String fileName = files[i];
                if (!fileName.contains(".")) {
                    if (0 == assetDir.length()) {
                        copyAssertDirToData(context, fileName, dir + fileName + "/");
                    } else {
                        copyAssertDirToData(context, assetDir + "/" + fileName, dir +"/"+fileName);
                    }
                    continue;
                }
                File outFile = new File(mWorkingPath, fileName);
                if (outFile.exists()) {
                    outFile.delete();
                }
                InputStream in = null;
                if (0 != assetDir.length()){
                    in = context.getAssets().open(assetDir + "/" + fileName);
                }else{
                    in = context.getAssets().open(fileName);
                }
                OutputStream out = new FileOutputStream(outFile);
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
                //权限被拒绝
            } else if (resultCode == PermissionsActivity.PERMISSIONS_GRANTED) {
                //权限被授予
            }
            goMain();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        hideView();

    }

    private void hideView() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        contentMain.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }
}